<%@page import="entity.Admin" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
  String path = request.getContextPath();
  Admin admin = (Admin) session.getAttribute("admin");
  String deptName = "";
  if ("部门".equals(admin.getIsSuper())) {
    deptName = admin.getDeptName();
  }
%>

<div class="callout callout-left">
  <span class="callout-title text-muted">用户激活情况</span>
  <!-- &nbsp;<span class="glyphicon glyphicon-question-sign"></span> -->
</div>

<div class="callout">
  <form class="form-inline" action="" method="post">
    <div class="form-group">
      <label for="userName_search">姓名：</label>
      <input type="text" class="form-control" id="userName_search" name="userName_search" placeholder="用户账号"
             autocomplete="off"/>
    </div>
    <div class="form-group">
      <label for="softName_search">软件名称：</label>
      <select class="form-control" name="softName_search" id="softName_search">
        <option value="">全部</option>
      </select>
    </div>
    <div class="form-group">
      <label for="status_search">状态：</label>
      <select class="form-control" name="status_search" id="status_search">
        <option value="">全部</option>
        <option value="已激活">已激活</option>
        <option value="激活失败">激活失败</option>
      </select>
    </div>&nbsp;&nbsp;
    <a href="javascript:;" class="btn btn-primary" id="doSearch">
      <span class="glyphicon glyphicon-search">&nbsp;搜索</span>
    </a>&nbsp;&nbsp;&nbsp;
  </form>
</div>

<div class="callout callout-white">
  <div class="table-responsive">
    <table class="table table-hover">
      <thead>
      <tr>
        <th>编号</th>
        <th>用户账号</th>
        <th>软件名称</th>
        <th>激活日期</th>
        <th>激活到期日</th>
        <th>IP地址</th>
        <th>MAC地址</th>
        <th>机器码</th>
        <th>操作系统版本</th>
        <th>激活状态</th>
      </tr>
      </thead>
      <tbody id="act_content">
      </tbody>
    </table>
  </div>
</div>

<script>
require.config({
  baseUrl: '<%=path %>/js',
  paths: {
    jquery: 'jquery-1.9.1.min',
    commmon: 'common',
    bootstrapswitch: 'bootstrap-switch.min',
  }
});
require(['jquery', 'common'], function ($, common) {

var workspace = function () {

  var dataOpt;

  var userActiveSofts;
  var softwares;

  var init = function () {
    dataOpt = common.dataOpt;
    softwares = common.ajaxLoadData(dataOpt.softwareOpt);
    dataOpt.userActiveSoftOpt.data = {"deptName": encodeURI(encodeURI("<%=deptName %>"))}

    _initUserActiveSoftTables();
    _searchInit();
  }

  var _initUserActiveSoftTables = function () {

    userActiveSofts = common.ajaxLoadData(dataOpt.userActiveSoftOpt);
    dataOpt.userActiveSoftOpt.data = {"deptName": encodeURI(encodeURI("<%=deptName %>"))}

    var template = "<tr><th>#row#</th>" +
      "<td>#userName#</td>" +
      "<td>#softName#</td>" +
      "<td>#reqDate#</td>" +
      "<td>#expireDate#</td>" +
      "<td>#ip#</td>" +
      "<td>#MAC#</td>" +
      "<td>#machineId#</td>" +
      "<td>#os#</td>" +
      "<td><span name='status_text'>#status#</span></td></tr>";

    var row = 0;
    var htmlArr = [];
    var thisT = "";
    var html = "";
    for (var i = 0, n = userActiveSofts.length; i < n; i++) {
      thisT = template;
      thisT = thisT.replace(/#row#/g, ++row).replace(/#softName#/g, userActiveSofts[i]["softName"]).replace(/#userName#/g, userActiveSofts[i]["userName"]).replace(/#reqDate#/g, userActiveSofts[i]["reqDate"])
          .replace(/#expireDate#/g, userActiveSofts[i]["expireDate"]).replace(/#ip#/g, userActiveSofts[i]["ip"]).replace(/#MAC#/g, userActiveSofts[i]["MAC"]).replace(/#machineId#/g, userActiveSofts[i]["machineId"])
          .replace(/#os#/g, userActiveSofts[i]["os"]).replace(/#status#/g, userActiveSofts[i]["status"]);
      html += thisT;
      if ((row !== 0) && (row % common.commData.pageNum === 0)) {
        htmlArr.push(html);
        html = "";
      }
    }
    if ("" !== html) {
      htmlArr.push(html);// 插入不满pageNum的几条数据
    }


    var opt = {
      htmlArr: htmlArr,
      content: $("#act_content"),
    }
    common.pageNav(opt, function () {
      $("[name='status_text']").each(function () {
        if ("已激活" === $(this).html()) {
          $(this).addClass("text-success");
        } else {
          $(this).addClass("text-danger");
        }
      });
    });
  }

  var _searchInit = function () {
    // 软件名称
    softwares.forEach(function (software) {
      var opt = document.createElement("option");
      $(opt).val(software["softName"]);
      $(opt).text(software["softName"]);
      $("#softName_search").append(opt);
    });

    $("#doSearch").click(function () {
      dataOpt.userActiveSoftOpt.data = {
        "userName": encodeURI(encodeURI($("#userName_search").val())),
        "softName": encodeURI(encodeURI($("#softName_search").val())),
        "status": encodeURI(encodeURI($("#status_search").val())),
      }
      _initUserActiveSoftTables();
    });
  }

  return {
    init: init
  }
}();

$(function () { workspace.init(); });

});
requirejs.onError = function (err) {
  console.error(err);
  if (err.requireType === 'timeout') {
    console.error('modules: ' + err.requireModules);
  }
  throw err;
}
</script>